package com.i8office.android.zhihuixinwu.module.presenter;

import com.cyan.tools.callback.SimpleCallback;
import com.i8office.android.zhihuixinwu.bean.Preview;
import com.i8office.android.zhihuixinwu.module.contract.PreviewContract;
import com.i8office.android.zhihuixinwu.module.model.PreviewModel;

/**
 * Created by pc on 2018/7/7.
 */

public class PreviewPresenter extends BasePresenterImpl<PreviewContract.View> implements PreviewContract.Presenter {
    PreviewContract.Model model;

    public PreviewPresenter(PreviewContract.View view) {
        super(view);
        model = new PreviewModel();
    }

    @Override
    public void getPreview(String id) {
        addDisposable(model.getPreview(new SimpleCallback<Preview>(mView) {
            @Override
            public void requestSuccess(Preview preview) {
                if (preview.getStatus() == -1)
                    mView.showMessage(preview.getMessage());
                else if (preview.getStatus() == 0)
                    mView.waitResult();
                else
                    mView.showPreview(preview);
            }

            @Override
            public void requestError(String msg) {
                super.requestError(msg);
                mView.toast(msg);
                mView.showError();

            }
        }, id));
    }

    @Override
    public void getNewsPreviewResult(String id) {
        addDisposable(model.getNewsPreviewResult(new SimpleCallback<Preview>(mView) {
            @Override
            public void requestSuccess(Preview preview) {
                if (preview.getStatus() == -1)
                    mView.showMessage(preview.getMessage());
                else if (preview.getStatus() == 0)
                    mView.waitResult();
                else
                    mView.showPreview(preview);
            }

            @Override
            public void requestError(String msg) {
                super.requestError(msg);
                mView.toast(msg);
                mView.showError();

            }
        }, id));
    }

    @Override
    public void getPreview(String id, String fileName) {
        if(fileName.endsWith(".xlsx")||fileName.endsWith(".xls")){
            addDisposable(model.getExcelPreview(new SimpleCallback<Preview>(mView) {
                @Override
                public void requestSuccess(Preview preview) {
                    if (preview.getStatus() == -1)
                        mView.showMessage(preview.getMessage());
                    else if (preview.getStatus() == 0)
                        mView.waitResult();
                    else
                        mView.showPreview(preview);
                }

                @Override
                public void requestError(String msg) {
                    super.requestError(msg);
                    mView.toast(msg);
                    mView.showError();

                }
            }, id));
        }else if(fileName.endsWith(".ceb")){
            addDisposable(model.getCebPreview(new SimpleCallback<Preview>(mView) {
                @Override
                public void requestSuccess(Preview preview) {
                    if (preview.getStatus() == -1)
                        mView.showMessage(preview.getMessage());
                    else if (preview.getStatus() == 0)
                        mView.waitResult();
                    else
                        mView.showPreview(preview);
                }

                @Override
                public void requestError(String msg) {
                    super.requestError(msg);
                    mView.toast(msg);
                    mView.showError();

                }
            }, id));
        }else{
            getPreview(id);
        }
    }
}
